package com.medicine.web.controller.manage;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.medicine.common.core.domain.entity.SysUser;
import com.medicine.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.medicine.common.annotation.Log;
import com.medicine.common.core.controller.BaseController;
import com.medicine.common.core.domain.AjaxResult;
import com.medicine.common.enums.BusinessType;
import com.medicine.manage.domain.OrderProcess;
import com.medicine.manage.service.IOrderProcessService;
import com.medicine.common.utils.poi.ExcelUtil;
import com.medicine.common.core.page.TableDataInfo;

/**
 * 流程Controller
 * 
 * @author admin
 * @date 2024-10-13
 */
@RestController
@RequestMapping("/manage/process")
public class OrderProcessController extends BaseController
{
    @Autowired
    private IOrderProcessService orderProcessService;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询流程列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OrderProcess orderProcess)
    {
        System.out.println("orderProcess");
        startPage();
        List<OrderProcess> list = orderProcessService.selectOrderProcessList(orderProcess);
        return getDataTable(list);
    }

    @GetMapping("/listAndDetail")
    public TableDataInfo listAndDetail(OrderProcess orderProcess)
    {
        System.out.println("orderProcess");
        List<OrderProcess> list = orderProcessService.selectOrderProcessListAndDetail(orderProcess);
        return getDataTable(list);
    }

    @GetMapping("/getProcessByOrderIdAndType")
    public AjaxResult getProcessByOrderIdAndType(Long orderId, Long processType){
        OrderProcess orderProcess = new OrderProcess();
        orderProcess.setOrderId(orderId);
        orderProcess.setProcessType(processType);
        List<OrderProcess> list = orderProcessService.selectOrderProcessListAndDetail(orderProcess);
        for (OrderProcess process : list) {
            if(process.getStartTime()!=null && process.getEndTime()!=null){
                long time = process.getEndTime().getTime() - process.getStartTime().getTime();
                process.setTime(time/1000);
            }
            SysUser sysUser = userService.selectUserById(new Long(process.getCreateBy()));
            if(sysUser!=null){
                System.out.println(sysUser.getNickName());
                process.setOperator(sysUser.getNickName());
                System.out.println(process);
            }

        }

        return AjaxResult.success(list.get(0));
    }

    /**
     * 导出流程列表
     */
    @PreAuthorize("@ss.hasPermi('manmage:process:export')")
    @Log(title = "流程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderProcess orderProcess)
    {
        List<OrderProcess> list = orderProcessService.selectOrderProcessList(orderProcess);
        ExcelUtil<OrderProcess> util = new ExcelUtil<OrderProcess>(OrderProcess.class);
        util.exportExcel(response, list, "流程数据");
    }

    /**
     * 获取流程详细信息
     */
    @PreAuthorize("@ss.hasPermi('manmage:process:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(orderProcessService.selectOrderProcessById(id));
    }

    /**
     * 新增流程
     */

    @Log(title = "流程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderProcess orderProcess)
    {
        return toAjax(orderProcessService.insertOrderProcess(orderProcess));
    }

    /**
     * 修改流程
     */

    @Log(title = "流程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderProcess orderProcess)
    {
        return toAjax(orderProcessService.updateOrderProcess(orderProcess));
    }


    /**
     * 修改流程
     */

    @Log(title = "流程", businessType = BusinessType.UPDATE)
    @PutMapping("/appEdit")
    public AjaxResult appEdit(@RequestBody OrderProcess orderProcess)
    {
        return toAjax(orderProcessService.updateProcessStatusByOrderAndType(orderProcess));
    }

    /**
     * 删除流程
     */
    @PreAuthorize("@ss.hasPermi('manmage:process:remove')")
    @Log(title = "流程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(orderProcessService.deleteOrderProcessByIds(ids));
    }
}
